<?php

new Db('localhost', 'www', '555', 'jsonrpc', 'jsonrpc_');

function db_query()          { return call_user_func_array(array($GLOBALS['db'], 'query')         , func_get_args()); }
function db_error()          { return call_user_func_array(array($GLOBALS['db'], 'error')         , func_get_args()); }
function db_fetch_row()      { return call_user_func_array(array($GLOBALS['db'], 'fetch_row')     , func_get_args()); }
function db_fetch_array()    { return call_user_func_array(array($GLOBALS['db'], 'fetch_array')   , func_get_args()); }
function db_fetch_object()   { return call_user_func_array(array($GLOBALS['db'], 'fetch_object')  , func_get_args()); }
function db_affected_rows()  { return call_user_func_array(array($GLOBALS['db'], 'affected_rows') , func_get_args()); }
function db_result()         { return call_user_func_array(array($GLOBALS['db'], 'result')        , func_get_args()); }
function db_last_insert_id() { return call_user_func_array(array($GLOBALS['db'], 'last_insert_id'), func_get_args()); }


$variable = new KVStore('variable', 'VARCHAR(100)');

function variable_get() { return call_user_func_array(array($GLOBALS['variable'], 'get'), func_get_args()); }
function variable_set() { return call_user_func_array(array($GLOBALS['variable'], 'set'), func_get_args()); }
function variable_del() { return call_user_func_array(array($GLOBALS['variable'], 'del'), func_get_args()); }




function get_messages($bClear = FALSE) {
	$messages = $_SESSION['messages'];
	if ($bClear)
		unset($_SESSION['messages']);
	return $messages;
}

function set_message($message) {
	$_SESSION['messages'][] = $message;
}

db_query("SHOW TABLES LIKE '{cache}'");
if (!db_affected_rows()) {
	db_query("
		CREATE TABLE {cache} (
			cid VARCHAR(100) PRIMARY KEY,
			key_sql LONGTEXT CHARACTER SET utf8,
			key_md5 VARCHAR(64),
			data LONGBLOB,
			INDEX idx_key_md5 (key_md5)
		)
	");
}
























